﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace CRMWeb.Utilities
{
    public static class DateTimeHelper
    {
        private static DateTime UNIX_EPOCH = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);

        public static DateTime UnixToDateTime(long value)
        {
            return UNIX_EPOCH.AddSeconds(Convert.ToDouble(value)).ToLocalTime();
        }

        public static long DateTimeToUnix(DateTime value)
        {
            return Convert.ToInt64((value.ToUniversalTime() - UNIX_EPOCH).TotalSeconds);
        }

        public static long ToUnix(this DateTime value)
        {
            return DateTimeToUnix(value);
        }

        public static string DateTimeToString(DateTime? dateTime)
        {
            return dateTime.HasValue ? dateTime.Value.ToString("yyyy-MM-dd HH:mm:ss.fff") : string.Empty;
        }

        public static string DateTimeToString(DateTime dateTime)
        {
            return dateTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
        }
    }
}